<?php
/**
* object-oriented file system manipulation as nodes.
* This file belongs to a webappkit package (kit)
* @link http://webappkit.net
* @package pathnodes
* @subpackage w3clog
* @author J.Ducastel <jeremie@ducastel.name>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

/**
* W3C-like log file node
* @link http://www.w3.org/TR/WD-logfile
*/
class w3cLogNode extends fileNode {
/*------------------------------------------------------------------------------
                                                                     constructor
------------------------------------------------------------------------------*/
	/**
	* constructor
	* @access public
	*/
	function w3cLogNode($path) {
		$this->__construct($path);
	}/**/
	
	/**
	* real constructor
	* @access public
	*/
	function __construct($path) {
		$path.='.'.date('Y-m-d').'.log';
		parent::__construct($path);
	}/**/
/*------------------------------------------------------------------------------
                                                                  public methods
------------------------------------------------------------------------------*/
	/**
	* write a new line.
	* may assigns autovalue for some fields, if a method named auto_{field} exists it will be called
	* @param array $data associative array field => value
	* @access public
	* @return bool
	*/
	function writeLine($data=array()) {
		// writing header ?
		if (!$this->exists())
			$this->writeHeader();
		// merging provided values with auto values
		$line=array();
		foreach ($this->fields as $field) {
			$method='auto_'.str_replace('-','_',$field);
			if (isset($data[$field]))
				$line[$field]=$data[$field];
			else if (method_exists($this,$method))
				$line[$field]=$this->$method();
		}
		// building line string
		$line=join(chr(9),$line).chr(13).chr(10);
		// opening in write mode, going at end
		//$this->handle('w');
		$this->toEnd();
		// writing line
		return $this->write($line);
	}/**/
	
	/**
	* write log file header
	* @access public
	* @return bool
	*/
	function writeHeader() {
		$this->handle('w+');
		$header=$this->makeHeader(); //echo $header;
		return $this->write($header);
		
	}/**/
	
	/**
	* 
	* @param
	* @access public
	* @return bool
	* /
	function setFields($data=array()) {
		$this->fields=$data;
		return true;
	}/**/
	
	/**
	* adds a field
	* @param string $field
	* @access public
	* @return bool
	*/
	function addField($field) {
		$this->fields[]=$field;
	}/**/
	/**
	* @var string for header
	*/
	var $software;
	/**
	* @var array remarks header lines
	*/
	var $remarks=array();
	/**
	* @var string start date
	*/
	var $start_date;
	/**
	* @var array log fields
	*/
	var $fields=array(
		'cs-uri');
	/*
	* @var array some standard fields
	*/
	var $types=array(
		'date' // yyyy-mm-dd
		, 'time' // HH:ii:ss
		, 'time-taken' // float, seconds
		, 'bytes' // integer, bytes transferred
		, 'cached' // 0|1
		, 'c-ip' // ip du client
		, 's-status' // code de reponse http
		, 'cs-method' // GET|POST
		, 'cs-uri' // chaine de requete complete
		, 'cs-uri-stem' // uri sans variables
		, 'cs-uri-query' // variables GET
		, 's-comment' // commentaire
	);
	/**
	* @var array values for next line
	*/
	var $next_line=array();

	/**
	* builds header
	* @access private
	* @return string
	*/
	function makeHeader() {
		$endline=chr(13).chr(10);
		// version : obligatoire
		$header='#Version: 1.0'.$endline;
		// software, remark : facultatif
		if ($this->software)
			$header.='#Software: '.$this->software.$endline;
		foreach ($this->remarks as $remark)
			$header.='#Remark: '.$remark.$endline;
		$header.='#Remark: this is a w3c-like log file, see http://www.w3.org/TR/WD-logfile.html'.$endline;
		// start-date : facultatif
		$header.='#Start-Date: '.date('Y-m-d H:i:s').$endline
		// fields : obligatoire
		.'#Fields: '.join(chr(9),$this->fields).$endline;
		return $header;
	}
	
	/**
	* automatic value for fields cs_uri = Client-server URI
	* @access private
	* @return string
	*/
	function auto_cs_uri() {
		return $_SERVER['REQUEST_URI'];
	}/**/
}
?>
